﻿Public Structure strctEmpInfo
    Public EmpCd As String
    Public EmpName As String
End Structure

Public Class CEmpDAO
    Private Shared m_instance As CEmpDAO = New CEmpDAO
    Private m_EmpHashMap As New Hashtable

    Private Const CSV_FILENAME = "master\emplist.csv"

    Public Shared Function GetInstance() As CEmpDAO
        Return m_instance
    End Function

    Public Sub New()
        Dim CSVFileName As String = CApplicationManager.GetRealPath(CSV_FILENAME, CApplicationManager.EnumSpecialFolder.PATH_MYDOCUMENT)
        If Not System.IO.File.Exists(CSVFileName) Then
            Debug.WriteLine("CSVファイルが存在しないためコピーして作成します。")
            Dim strAppDir As String = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)
            Dim BaseCSVFileName As String = System.IO.Path.Combine(strAppDir, "dbf\csv\empList.csv")
            Dim CSVPathName As String = CApplicationManager.GetRealPath("master", CApplicationManager.EnumSpecialFolder.PATH_MYDOCUMENT)
            System.IO.Directory.CreateDirectory(CSVPathName)
            System.IO.File.Copy(BaseCSVFileName, CSVFileName)
        End If

        LoadCSV()
    End Sub

    Public Sub LoadCSV()
        Dim CSVFileName As String = CApplicationManager.GetRealPath(CSV_FILENAME, CApplicationManager.EnumSpecialFolder.PATH_MYDOCUMENT)

        Try
            m_EmpHashMap.Clear()

            '' CSV読み込み
            Debug.Write("CSV[" & CSVFileName & "] Reading....")
            Dim Reader As New System.IO.StreamReader(CSVFileName, System.Text.Encoding.GetEncoding("UTF-8"))
            Dim line As String
            Dim lineCols() As String
            Dim EmpInfo As strctEmpInfo
            Dim firstFlag As Boolean = True

            While Not Reader.EndOfStream
                line = Reader.ReadLine()
                If firstFlag Then
                    firstFlag = False
                Else
                    lineCols = line.Split(vbTab)
                    EmpInfo.EmpCd = lineCols(0)     '' 従業員番号
                    EmpInfo.EmpName = lineCols(1)   '' 氏名
                    m_EmpHashMap.Add(EmpInfo.EmpCd, EmpInfo)
                End If

            End While

            Reader.Close()
            Debug.Write("OK")
        Catch ex As Exception
            Debug.Write("ERR:" & ex.ToString)
        Finally
        End Try
        Debug.WriteLine("")
    End Sub

    Public Function GetEmpInfo(ByVal EmpCd As String) As strctEmpInfo
        Dim result As strctEmpInfo
        result = m_EmpHashMap(EmpCd)
        Return result
    End Function

    Public Function MasterUpdate() As Boolean
        Dim CSVFileName As String = CApplicationManager.GetRealPath(CSV_FILENAME, CApplicationManager.EnumSpecialFolder.PATH_MYDOCUMENT)
        Dim WebMng As CWebManager = CWebManager.GetInstance()

        Try
            Dim downResult As String = WebMng.fileDownload(CSVFileName, EnumFileType.EMP)
            Debug.WriteLine(downResult)
        Catch ex As Exception
            Debug.WriteLine("ERROR")
            Debug.WriteLine(ex)
            Return False
        End Try
        LoadCSV()
        Return True
    End Function


End Class
